<template>
  <div>
    <!-- 轮播图 -->
    <van-swipe class="my-swipe" :autoplay="3000" indicator-color="darkred">
      <van-swipe-item v-for="i in gallery" :key="i.id">
        <img :src="i.img_url" alt />
      </van-swipe-item>
    </van-swipe>
    <tips></tips>
    <div class="info">
      <h3>{{info.name}}</h3>
      <p>{{info.goods_brief}}</p>
      <p class="price">{{info.retail_price | filterMoney}}</p>
    </div>
    <van-cell class="myCell" title="展示弹出层" @click="show=true" is-link />
    <!-- 商品参数 -->
    <div class="attribute">
      <h4>商品参数</h4>
      <ul>
        <li v-for="(i, index) in attribute" :key="index">
          <span>{{i.name}}</span>
          <section class="van-ellipsis">{{i.value}}</section>
        </li>
      </ul>
    </div>
    <!-- 信息展示 -->
    <div class="box" ref="box"></div>
    <!-- 常见问题 -->
    <div class="title">
      <span>常见问题</span>
    </div>
    <!-- 问题详情 -->
    <ul class="issue">
      <li v-for="i in issue" :key="i.id">
        <h3>{{i.question}}</h3>
        <p>{{i.answer}}</p>
      </li>
    </ul>
    <!-- 大家都在看 -->
    <div class="title">
      <span>大家都在看</span>
    </div>
    <!-- 大家看的内容 -->
    <Product :goodsList="goodsList"></Product>
    <!-- sku -->
    <van-sku ref="sku" v-model="show" :sku="sku" :goods="goods" :hide-stock="sku.hide_stock" />
    <!-- 商品导航组件 -->
    <MyGoodsActions :badgeNum="badgeNum" @addToCart="addToCart"></MyGoodsActions>
  </div>
</template>

<script>
import { getProductDetail, getProductRelated } from '@/api/product.js'
import { AddToCard, GetCartNum } from '@/api/cart.js'
import tips from '../components/tips'
import Product from '../components/Product'
import MyGoodsActions from '../components/MyGoodsActions'
export default {
  components: {
    tips,
    Product,
    MyGoodsActions
  },
  data () {
    return {
      // 产品详情id
      detailId: '',
      // 轮播图
      gallery: [],
      // 产品信息
      info: {},
      // 商品参数
      attribute: [],
      // 常见问题
      issue: [],
      // 大家再看的数据
      goodsList: [],
      productList: [],
      // sku的显示状态
      show: false,
      goods: {
        // 默认商品 sku 缩略图
        picture: ''
      },
      sku: {
        tree: [],
        // 剩余商品库存
        hide_stock: false,
        // 价格
        price: '',
        // 商品总库存
        stock_num: 0
      },
      // 购物车的数量
      badgeNum: 0
    }
  },
  methods: {
    // 获取产品详情
    async getProductDetailInfo () {
      const res = await getProductDetail({
        id: this.detailId
      })
      console.log(res)
      if (res.errno === 0) {
        const { gallery, info, attribute, issue, productList } = res.data
        this.gallery = gallery
        this.info = info
        this.attribute = attribute
        this.issue = issue
        // 图片
        this.$refs.box.innerHTML = info.goods_desc
        // sku的picture
        this.goods.picture = info.list_pic_url
        // 价格
        this.sku.price = info.retail_price.toFixed(2)
        // 库存
        this.sku.stock_num = info.goods_number
        this.productList = productList
      }
    },
    // 获取相关产品
    async getProductRelated () {
      const res = await getProductRelated({
        id: this.detailId
      })
      if (res.errno === 0) {
        const { goodsList } = res.data
        this.goodsList = goodsList
      }
    },
    // 获取购物车数量
    async GetCartNum () {
      const res = await GetCartNum()
      this.badgeNum = res.data.cartTotal.goodsCount
    },
    // 加入购物车
    async addToCart () {
      // 判断sku是否显示
      if (this.show) {
        // 处于显示 直接加入购物车
        // this.$toast('可以加入购物车')
        const num = this.$refs.sku.getSkuData().selectedNum
        const res = await AddToCard({
          goodsId: this.detailId,
          productId: this.productList[0].id,
          number: num
        })
        if (res.errno === 0) {
          this.badgeNum = res.data.cartTotal.goodsCount
          this.$toast('加入购物车成功')
          this.$router.push('/cart')
        }
      } else {
        // sku显示
        this.show = true
      }
    }
  },
  created () {
    this.detailId = this.$route.query.id
    this.getProductDetailInfo()
    this.getProductRelated()
    this.GetCartNum()
  }
}
</script>

<style lang="less" scoped>
/deep/.van-sku-container {
  min-height: auto;
}
.van-goods-action {
  z-index: 9999;
}
.issue {
  padding: 0 4%;
  li {
    h3 {
      height: 0.3rem;
      line-height: 0.3rem;
      position: relative;
      font-weight: normal;
      padding-left: 0.2rem;
      &::before {
        content: "";
        width: 4px;
        height: 4px;
        background-color: darkred;
        display: inline-block;
        position: absolute;
        top: 50%;
        margin-top: -2px;
        left: 0;
      }
    }
    p {
      font-size: 0.12rem;
      line-height: 0.2rem;
      padding-left: 0.2rem;
    }
  }
}
.title {
  width: 100%;
  background-color: #fff;
  height: 0.5rem;
  position: relative;
  &::before {
    content: "";
    width: 50%;
    height: 0.02rem;
    background-color: #ccc;
    position: absolute;
    top: 50%;
    margin-top: -0.01rem;
    left: 50%;
    margin-left: -25%;
  }
  span {
    width: 30%;
    display: block;
    background-color: #fff;
    position: relative;
    text-align: center;
    margin: 0 auto;
    height: 0.5rem;
    line-height: 0.5rem;
  }
}
/deep/.box {
  img {
    width: 100%;
    display: block;
  }
}
img {
  width: 100%;
  display: block;
}
.info {
  background-color: #fff;
  text-align: center;
  padding: 0.2rem 0;
  border-bottom: 1px solid #f1f1f1;
  h3 {
    font-weight: normal;
    font-size: 0.2rem;
    line-height: 0.5rem;
  }
  p {
    color: #949494;
    font-size: 0.12rem;
    line-height: 0.3rem;
  }
  .price {
    color: darkred;
    font-size: 0.12rem;
    line-height: 0.3rem;
  }
}
.myCell {
  margin-bottom: 0.2rem;
}
.attribute {
  background-color: #fff;
  padding: 0.2rem 4%;
  h4 {
    font-weight: normal;
    font-size: 0.16rem;
    margin-bottom: 0.2rem;
  }
  ul {
    li {
      background-color: #efefef;
      border-radius: 0.03rem;
      display: flex;
      height: 0.4rem;
      line-height: 0.4rem;
      margin-bottom: 0.1rem;
      justify-content: space-between;
      span {
        width: 20%;
        text-align: right;
      }
      section {
        flex: 1;
        padding-left: 0.25rem;
      }
    }
  }
}
</style>
